<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm"  :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="流程名称" prop="instanceName">
              <el-input v-model="queryParams.instanceName" placeholder="请输入流程名称" clearable style="width: 240px"/>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search"  @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh"  @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-check" v-hasPermi="['archive:archiveApproval:batchApproval']" @click="agree">批量同意</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-close" v-hasPermi="['archive:archiveApproval:batchDisagree']" @click="disagree">批量不同意</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table :data="tableDataList" @selection-change="handleSelectionChange" border align="center" header-align="center" :height="tableHeight" v-loading="loading">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="序号" type="index" :index="table_index" width="55" align="center" prop="id"/>
          <el-table-column prop="instanceName" label="流程名称" align="center"></el-table-column>
          <el-table-column prop="definitionKey" label="流程类型" align="center"></el-table-column>
          <el-table-column prop="name" label="任务节点名称" align="center"></el-table-column>
          <el-table-column prop="assignee" label="办理人" align="center"></el-table-column>
          <el-table-column prop="startTime" label="创建时间" align="center" :formatter="formattedTime"></el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button  type="text" icon="el-icon-edit" @click="handleApprove(scope.row)">审批</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 审批页面 -->
        <!-- 归档申请单 istype="1"   待办 istype="2"  已办 istype="3" -->
        <approve :title="title" :open="approveOpen" istype="2" @closeDelDialog="closeDelDialog" ref="approveRef"></approve>
    </div>
</template>
<script>
import estimateTableHeight from '@/mixins/estimateTableHeight'
import {formDataSaveAll, getProcessType, taskList} from "@/api/archive/fileCollection/archiveApproval";
import approve from "@/views/archive/fileCollection/archiveApproval/notQrocessed/approve";
import {getTaskById} from "@/api/activiti/task";
//vue项目中element-tabel自动计算高度
export default {
    components: { approve },
    mixins: [estimateTableHeight], // 混入方法
    data() {
        return {
          queryParams: {
            pageNum:1,
            pageSize:10,
          },
          showSearch: true, // 显示搜索条件
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 表格数据
          tableDataList: [],
          loading: true,
          total: 0,
          // 流程类型
          typeOption: [],
          // 审批页面
          approveOpen: false,
          title: '审批'
        }
    },
    created() {
        this.getOption()
        this.getList()
        if (this.$route.query.taskId != undefined) {
          getTaskById(this.$route.query.taskId).then(res=>{
            this.handleApprove(res)
          })
        }
        if(this.$route.query.item != undefined) {
          if (this.$route.query.item.id != undefined) {
            this.handleApprove(this.$route.query.item)
          }
        }
    },
    methods: {
      formattedTime(row,column) {
        return new Date(row.startTime).toLocaleString();
      },
      getOption() {
        getProcessType().then(response=>{
          const data = response.data
          for (const key in data) {
            this.typeOption.push({
              value: key,
              label: data[key]
            });
          }
        })
      },
      table_index(index){
        return (this.queryParams.pageNum-1) * this.queryParams.pageSize + index + 1;
      },
      getList() {
        this.loading = true
        taskList(this.queryParams).then(response=>{
          this.tableDataList = response.rows
          for (let i = 0; i < response.rows.length; i++) {
            for (let j = 0; j < this.typeOption.length; j++) {
              if(response.rows[i].definitionKey == this.typeOption[j].value) {
                this.tableDataList[i].definitionKey = this.typeOption[j].label
              }
            }
          }
          this.total = response.total
          this.loading = false
        })
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id);
        this.single = selection.length != 1;
        this.multiple = !selection.length;
      },
      // 审批
      handleApprove(row) {
        this.$nextTick(() => {
          this.$refs.approveRef.init(row);
        })
        this.approveOpen = true
      },
      //关闭
      closeDelDialog() {
        this.approveOpen = false
        this.getList();
      },
      // 搜索按钮
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      // 重置按钮操作
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 同意
      agree() {
        const id = this.ids
        if(id.length == 0) {
          this.$modal.msgWarning('请选择至少一条数据')
        } else {
          this.form = [{
            controlId: 'is_agree',
            controlDefault: '不同意--__--同意',
            controlIsParam: 'i',
            controlLable: '审批意见',
            controlType: 'radio',
            controlValue: 1
          }];
          this.form.id = id
          formDataSaveAll(this.form).then(response=>{
            this.$modal.msgSuccess(response.msg)
            this.getList()
          })
        }
      },
      // 不同意
      disagree() {
        const id = this.ids
        if(id.length == 0) {
          this.$modal.msgWarning('请选择至少一条数据')
        } else {
          this.form = [{
            controlId: 'is_agree',
            controlDefault: '不同意--__--同意',
            controlIsParam: 'i',
            controlLable: '审批意见',
            controlType: 'radio',
            controlValue: 0
          }];
          this.form.id = id
          formDataSaveAll(this.form).then(response=>{
            this.$modal.msgSuccess(response.msg)
            this.getList()
          })
        }
      }
  }
}
</script>
